package lerry.nape;

import flash.display.DisplayObjectContainer;
import flash.events.Event;
import lerry.nape.utils.Option;
import nape.geom.Vec2;
import nape.phys.Body;
import nape.space.Space;
import nape.util.Debug;

/**
 * ...
 * @author berry
 */
class NapeEngine {
	static public var options:Option;
	static public var space:Space;
	static public var onUpdate:Dynamic -> Void;
	static public var version(get_version,null):String;
	
	static public var source:DisplayObjectContainer;
	
	static public var paused:Bool;
	
	static private function get_version():String {
		return Debug.version();
	}
	
	public function new() {
		throw "can not be instanced!";
	}
	/**
	 * 初始化nape
	 * @param	gravity 重力
	 * @param	parent 容器
	 */
	static public function init(gravity:Vec2 = null, parent:DisplayObjectContainer):Void {
		
		space = new Space(gravity);
		source = parent;
		options = new Option(space,source);
	}
	
	/**
	 * 添加物体到nape引擎
	 * @param	b
	 */
	static public function add(b:Body):Void {
		b.space = space;
	}
	
	static private function step(deltaTime:Float,vec:Int=10,pos:Int=10):Void {
		space.step(deltaTime, vec, pos);
	}
	
	static public function update(e:Event):Void {
		if (!paused) {
			step(1 / source.stage.frameRate, 10, 10);
			options.updateDebug();
			options.updateGraphic();
			if (options.update!=null) {
				options.update(e);
			}
		}

		
		
	}
	/**
	 * 启动引擎
	 */
	static public function start():Void {
		source.addEventListener(Event.ENTER_FRAME, update);
	}
	/**
	 * 停止引擎
	 */
	static public function stop():Void {
		source.removeEventListener(Event.ENTER_FRAME, update);
	}
	static public function pause():Void {
		paused = true;
	}
	static public function resume():Void {
		paused = false;
	}
}